package com.example.weather;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 古嘉华 on 2018/4/26.
 */

public class RecyclerWeatherAdapter extends RecyclerView.Adapter<RecyclerWeatherAdapter.ViewHolder> {

    private ArrayList<String> mData;
    private ArrayList<Integer> imgData;
    private Context context;

    public RecyclerWeatherAdapter(Context context,ArrayList<String> data,ArrayList<Integer> imgData) {
        this.context = context;
        this.mData = data;
        this.imgData = imgData;
        notifyDataSetChanged();
    }

/*    public void updateData(ArrayList<String> data,ArrayList<Integer> imgData) {
        this.mData = data;
        this.imgData = imgData;
        notifyDataSetChanged();
    }*/

    @Override
    public RecyclerWeatherAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 实例化展示的view
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_weather, parent, false);
        // 实例化viewholder
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerWeatherAdapter.ViewHolder holder, int position) {
            // 绑定数据
            holder.item_tv.setText(mData.get(position));
//        Log.e("msg","Data:"+imgData);
            holder.item_img.setImageResource(imgData.get(position));
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView item_tv;
        private ImageView item_img;

        public ViewHolder(View itemView) {
            super(itemView);
            item_tv = itemView.findViewById(R.id.tv_re);
            item_img = itemView.findViewById(R.id.iv_img);
        }
    }
}
